<?php
/**
 * 订单管理模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class OrderAction extends CommonAction {
    
    /**
     * 列表
     * @see CommonAction::index()
     */
    public function index() {
    	//列表过滤器，生成查询Map对象
    	$map = $this->_search();
    	if (method_exists($this, '_filter')) {
    		$this->_filter($map);
    	}
    	$model = D('Order');
    	if (!empty($model)) {
    		$this->_list($model, $map , 'sort_order' , true);
    	}
    	$this->display();
    	return;
    }
	
    public function edit() {
        $model = M($this->getActionName());
        $id = $_REQUEST[$model->getPk()];
        $data = $model->find($id);
		
		$model=M('article');
		$data['title'] =$model->where('id='.$data['art_id'])->getField('title');
        $this->assign('data', $data);
        $this->display('add');
    }
	
	
	protected function _list($model, $map = array(), $sortBy = '', $asc = false) {
		$all_fields=$model->getDbFields();
        //排序字段 默认为主键名
        if (isset($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        } else {
            $order = !empty($sortBy) ? $sortBy : $model->getPk();
        }
        // 排序方式
        if (isset($_REQUEST['sort'])) {
            $sort = $_REQUEST['sort']=='asc' ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        // 按自定义排序
		if(in_array('sort_order',$all_fields)){
			$order = 'sort_order';
			$sort = 'asc';
		}
        //取得满足条件的记录数
        $count = $model->where($map)->count('id');
        import("ORG.Util.Page");
        //创建分页对象 listRows 每页显示数量
        if (!empty($_REQUEST['listRows'])) {
            $listRows = $_REQUEST['listRows'];
        } else {
            $listRows = '15';
        }
        $p = new Page($count, $listRows);
        //分页查询数据
        $list = $model->where($map)->order($order . ' ' . $sort)->limit($p->firstRow . ',' . $p->listRows)->select();
		$new_list = array();
		foreach($list as $k=>$v){
			$new_list[$k]	=	$v;
			$model=M('article');
			$new_list[$k]['title'] =$model->where('id='.$v['art_id'])->getField('title');
		}
		
		
        //分页显示
        $page = $p->show();
        //列表排序显示
        $sort = $sort == 'desc' ? 1 : 0;                     //排序方式
        
        //模板赋值显示
        $this->assign('listRows',$listRows);
        $this->assign('pagesize',$this->_getPageSize());
        $this->assign('list', $new_list);
        $this->assign('sort', $sort);
        $this->assign('order', $order);
        $this->assign("page", $page);
		$this->assign("map", $map);
        Cookie::set('_currentUrl_', __SELF__);
        return;
    }
}

?>